package com.xunshibao.core.dao.impl;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.transaction.support.TransactionTemplate;

import com.xunshibao.core.dao.GenericDao;

public class GenericDaoImpl implements GenericDao {

    protected HibernateTemplate hibernateTemplate;
    protected TransactionTemplate transactionTemplate;

    public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

    @Override
    public void delete(Object entity) {
        hibernateTemplate.delete(entity);
    }


    @Override
    public boolean save(Object entity) {
        try {
            hibernateTemplate.save(entity);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public Object saveOrUpdate(Object entity) {
        hibernateTemplate.saveOrUpdate(entity);
        return entity;
    }

    @Override
    public boolean update(Object entity) {
        try {
            hibernateTemplate.update(entity);
            return true;
        } catch (Exception ex) {
            return false;
        }
    }

    @Override
    public HibernateTemplate getHibernateTemplate() {
        return hibernateTemplate;
    }

    @Override
    public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
        this.hibernateTemplate = hibernateTemplate;
    }

    @SuppressWarnings("unchecked")
    @Override
    public List findByCriteria(DetachedCriteria criteria) {
        return hibernateTemplate.findByCriteria(criteria);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List findByCriteria(DetachedCriteria criteria, int firstResult, int maxResults) {
        return hibernateTemplate.findByCriteria(criteria, firstResult, maxResults);
    }

}
